#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>

int main()
{
    int fd[2];
    int ret = pipe(fd);
    if(ret < 0)
    {
        perror("pipe");
        return 0;
    }
    int flag = fcntl(fd[1],F_GETFL);
    fcntl(fd[1],F_SETFL,flag | O_NONBLOCK);
    flag = fcntl(fd[1],F_GETFL);
    pid_t pid = fork();
    if(pid < 0)
    {
        perror("fork");
        return 0;
    }
    else if(pid == 0)
    {
        //child
        //读端不关闭
        //读端关闭
        close(fd[0]);//关闭读
        int count = 0;
        while(1)
        {
            size_t size = write(fd[1],"i",1);
            if(size!=1)
            {
                printf("size = %ld",size);
                break;
            }
            printf("count:%d\n",++count);
        }
    }
    else 
    {
        //father
        close(fd[1]);//关闭写
        close(fd[0]);//关闭读
        while(1)
        {
            sleep(1);
        }
        
    }
}
